/***
This do-file creates 2 figures. 
The first is a binscatter of low-income employment vs workplace rent by state 
The second is a binscatter of net in-migration vs median rent by state 
***/

*-------------------------------------------------------------------------------
* Set up
*-------------------------------------------------------------------------------

* Set $root 
project figstabs, root
if (r(buildrunning)==0) include "${root}/code/config_interactive.do"

* Set globals
project, uses("${root}/code/set_globals.do")
include "${root}/code/set_globals.do"
local category "Employment"

* Create required subfolders
cap mkdir "${root}/data/derived/Covariates"
cap mkdir "${root}/results/Employment"

*-------------------------------------------------------------------------------
* Get net_in_migration by state for 2019 and 2021
*-------------------------------------------------------------------------------

* Open ASEC data 
project, uses("${root}/data/dvc/CPS/asec.dta")
use "${root}/data/dvc/CPS/asec.dta", clear 

* Keep only certain variables 
keep year statefip migsta1 migrate1 whymove asecwt 

* Create useful variables 

* Moved states flag 
gen moved_states = (migrate1 == 5) if !mi(migrate1)

* Collapse 

* Keep people who move States
keep if moved_states == 1 

* Get number of people who moved by state of origin and state of destination each year
gcollapse (sum) moved_states [pw = asecwt], by(statefip year migsta1)

* Round 
replace moved_states = round(moved_states, 1)

* Rename stuff 
rename statefip dest_state
rename migsta1 orig_state

* Get total in-migration for each state 
preserve

* Collapse 
collapse (sum) in_migration = moved_states, by(dest_state year)

* Tempfile 
tempfile in_migration
save `in_migration'
restore 

*-------------------------------------------------------------------------------
* Get total out-migration for each state 
*-------------------------------------------------------------------------------

* Collapse
collapse (sum) out_migration = moved_states, by(orig_state year)

* Merge in-migration
rename orig_state dest_state 
merge 1:1 dest_state year using `in_migration', nogen
rename dest_state state_fips 

* Get net-in-migration
gen net_in_migration = in_migration - out_migration

* Get state population 

* Dest_state pop 
project, uses("${root}/data/derived/ACS 2014-2018 5-Year State/ACS 2014-2018 State.dta")
merge m:1 state_fips using "${root}/data/derived/ACS 2014-2018 5-Year State/ACS 2014-2018 State.dta", keep(3) keepusing(pop_2014_2018) nogen // only state not merging in using is Puerto Rico
rename (pop_2014_2018) (pop)

* Create net-in-migration as a % of state population 
gen net_in_migration_pop = 100 * (net_in_migration / pop)

* Drop 2020 for now 
drop if year == 2020 

* Get 2019 in-migration on all 2021 rows 
gen temp = net_in_migration_pop if year == 2019 
bys state_fips: gegen net_in_migration_pop_2019 = mean(temp)
drop temp 

* Get difference in net-in-migration between 2021 and 2019 
gen diff_net_in_migration_pop = net_in_migration_pop - net_in_migration_pop_2019

* Merge rent
project, uses("${root}/data/derived/ACS 2014-2018 5-Year State/ACS 2014-2018 State.dta")
merge m:1 state_fips using "${root}/data/derived/ACS 2014-2018 5-Year State/ACS 2014-2018 State.dta", keep(1 3) nogen keepusing(state_fips med_2br_rent_2018)

rename (med_2br_rent_2018) (med_2br_rent)

*--------------------------------------------------------------------------------
* Create binscatter of net in-migration vs rent by state 2021
*--------------------------------------------------------------------------------

* Keep only first obs per state x year 
bys state_fips year: keep if _n == 1 
keep if year == 2019 | year == 2021 
keep if !mi(net_in_migration_pop)

* Regression
reg net_in_migration_pop med_2br_rent if year == 2021 [w = pop] , r
 
* Save regression results
local beta = _b[med_2br_rent] * 1000
local display_beta: display %4.2f `beta'
local std_err = _se[med_2br_rent] * 1000
local display_std_err: display %4.2f `std_err'
local text "text(-5 580 "Slope = `display_beta'%/$1000 (s.e. = `display_std_err')", place(ne) just(left) size(medlarge) color(gs8))"	

local x_title "Median Two Bedroom Monthly Rent in 2014-2018 by State ($)"
local scale "600(200)1800"
local xscale "580 1800"

* Binscatter
binscatter net_in_migration_pop med_2br_rent ///
if year == 2021 [w = pop] ///
	, reportreg  graphregion(m(r+8)) ///
	xtitle("`x_title'") ///
	xscale(range(`xscale')) ///
	xlabel(`scale') ///
	ytitle("Net In-Migration Share (%)" "in 2021") ///
	ylabel(-5 "-5%" 0 "0%" 5 "+5%",nogrid) ///
	xlab(600 "$600" 800 "$800" 1000 "$1,000" 1200 "$1,200" 1400 "$1,400" 1600 "$1,600" 1800 "$1,800") ///
	`text' ///
	${title_`version'}
oi_graph_export "${root}/results/Employment/Net In-Migration vs Median Rent Binscatter 2021", type(${fig_type}) 
